package client;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import communication.game.ConfirmGameFinished;

/**
 * Panel pour l'affichage du debriefing.
 * Il faut afficher le nom de la partie, son temps,
 * les scores (et les bonus utilises)
 * @author Jonathan Gander
 * @version 1.0
 */
public class JPanelDebriefing extends JPanel {
	/**
	 * Pour la serialisation
	 */
	private static final long serialVersionUID = -1624717659044392178L;

	private final ClientApplication clientApp;
	
	/**
	 * Partie terminee
	 */
	private ConfirmGameFinished gameFinished;
	
	/**
	 * Constructeur du panel
	 * @param clientApp L'application cliente
	 * @param gameFinished Objet de fin de jeu
	 */
	public JPanelDebriefing(ClientApplication clientApp, 
			ConfirmGameFinished gameFinished){
		
		
		this.clientApp = clientApp;
		this.gameFinished = gameFinished;
		
		int width = 350;
		int height = 350;
		
		this.setPreferredSize(new Dimension(width,height));
		
		// Titre
		JLabel lblTitle = new JLabel();
		lblTitle.setPreferredSize(new Dimension(width,20));
		lblTitle.setText("DEBRIEFING");
		lblTitle.setFont(new Font(lblTitle.getFont().getName(),
				lblTitle.getFont().getStyle(), 18));
		this.add(lblTitle);
		
		// Affichage du nom de la partie
		JLabel lblName = new JLabel();
		lblName.setPreferredSize(new Dimension(width,10));
		lblName.setText(gameFinished.getGame().getName());
		lblName.setFont(new Font(lblName.getFont().getName(),
				lblName.getFont().getStyle(), 12));
		this.add(lblName);
		
		// Affichage du temps de la partie)
		JLabel lblTime = new JLabel();
		lblTime.setPreferredSize(new Dimension(width,20));
		lblTime.setText(gameFinished.getTime() / 60 +" min");
		lblTime.setFont(new Font(lblTime.getFont().getName(),
				lblTime.getFont().getStyle(), 10));
		this.add(lblTime);
		
		// Affichage du classement
		JLabel lblClass = new JLabel();
		lblClass.setPreferredSize(new Dimension(width,20));
		lblClass.setText("Classement");
		lblClass.setFont(new Font(lblClass.getFont().getName(),
				lblClass.getFont().getStyle(), 12));
		this.add(lblClass);
		
		// Affichage des joueurs avec leur score
		JPanel panPlayers = new JPanel();
		int nbPlayers = gameFinished.getTabPlayers().length;
		panPlayers.setPreferredSize(new Dimension(width - 20,15 * nbPlayers));

		for (int i = 0; i < nbPlayers; i++) {
			JLabel lblPlayer = new JLabel();
			lblPlayer.setPreferredSize(new Dimension(200,10));
			lblPlayer.setText(gameFinished.getTabPlayers()[i].getPseudo());
			lblPlayer.setFont(new Font(lblPlayer.getFont().getName(),
					lblPlayer.getFont().getStyle(), 10));
			panPlayers.add(lblPlayer);
			
			JLabel lblScore = new JLabel();
			lblScore.setPreferredSize(new Dimension(100,10));
			lblScore.setText(""+gameFinished.getTabPlayers()[i].getScore());
			lblScore.setFont(new Font(lblScore.getFont().getName(),
					lblScore.getFont().getStyle(), 10));
			panPlayers.add(lblScore);
		}
		
		JScrollPane scpPlayers = new JScrollPane(panPlayers);
		scpPlayers.setPreferredSize(new Dimension(width,200));
		
		this.add(scpPlayers);
		
		// Affichage du bouton pour recommencer la partie
		if (gameFinished.getGame().getCreator().equals(clientApp.internals.userName)) {
			JButton btnRestart = new JButton("Redemarrer la partie");
			btnRestart.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent arg0) {

					// Redemarre la partie
					JPanelDebriefing.this.clientApp.internals.restartGame(
							JPanelDebriefing.this.gameFinished.getGame());
				}
				
			});
			this.add(btnRestart);
			
			JButton btnStopGame = new JButton("Fermer la partie");
			btnStopGame.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent arg0) {

					// Termine le jeu
					JPanelDebriefing.this.clientApp.internals.closeGame(
							JPanelDebriefing.this.gameFinished.getGame());
					
					JPanelDebriefing.this.clientApp.getClientView().showListGameDialog();

				}
				
			});
			this.add(btnStopGame);
			
		}
		else { // Un utilisateur normal
			JButton btnQuit = new JButton("Quitter la partie");
			btnQuit.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent arg0) {

					// Quitte la partie
					JPanelDebriefing.this.clientApp.internals.leaveGame(
							JPanelDebriefing.this.gameFinished.getGame(),
							JPanelDebriefing.this.clientApp.internals.userName);
					
					JPanelDebriefing.this.clientApp.getClientView().showListGameDialog();
					
				}
				
			});
			this.add(btnQuit);
		}
	}

}
